package org.cn.lennon.mybatis.transcation.impl.connection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

public class ConnestionPool {

	private DataSource dataSource;
	public static Connection connection = null;

	public static void initConnectionPool() {
		try {
			String driver = "com.mysql.cj.jdbc.Driver";
			String conn = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
			String userName = "root";
			String password = "";
			Class.forName(driver);
			connection = DriverManager.getConnection(conn, userName, password);
		} catch (ClassNotFoundException e) {
			// 无法完成jdbc mysql驱动加载，缺少包
			System.err.println("无法完成jdbc mysql驱动加载，缺少包");
		} catch (SQLException e) {
			// 获取不到数据库连接
			System.err.println("获取不到数据库连接");
		}
	}

	public static Connection getConnection() {
		// 这里暂时就不考虑是否有并发问题啦
		// TODO conncurent
		if (connection != null) {
			return connection;
		}
		initConnectionPool();
		return getConnection();
	}

	public static void colse() {
		if(connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
